import 'dart:collection';

import 'package:cloud_phone_manager/provider/AdbCurrentChangeProvider.dart';
import 'package:cloud_phone_manager/provider/AdbDevicesProvider.dart';
import 'package:cloud_phone_manager/utils/CmdUtils.dart';
import 'package:cloud_phone_manager/utils/ConnectUtils.dart';
import 'package:cloud_phone_manager/utils/NetManager.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

import '../data/PhoneFile.dart';

/// 设备列表
class RuichiChangeView extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _RuichiChangeView();
}

class _RuichiChangeView extends State<RuichiChangeView> {
  List<PhoneFile> _list = [];
  bool _isInit = false;
  String sn = ConnectUtils().getCurrentChooseDevices()?.sn??"";
  bool _mounted = false;
  AdbCurrentChangeProvider? provider;
  TextEditingController _countryTextEditingController = TextEditingController();
  TextEditingController _modelTextEditingController = TextEditingController();
  TextEditingController _gaidTextEditingController = TextEditingController();
  TextEditingController _versionTextEditingController = TextEditingController();
  TextEditingController _numberTextEditingController = TextEditingController();
  TextEditingController _gaidTypeTextEditingController =
      TextEditingController();
  bool _isRequestChangeDevice = false;

  @override
  void initState() {
    super.initState();
    _mounted = true;
    if (!_isInit) {
      _isInit = true;
      CmdUtils.runCommand(
          "adb shell ls -amlF", (success, error, process, successListener) {});
    }
  }

  @override
  void dispose() {
    _mounted = false;
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (provider == null) {
      provider = Provider.of<AdbCurrentChangeProvider>(context);
      debugPrint("注册监听");
      provider?.addListener(() {
        setState(() {
          sn = ConnectUtils().getCurrentChooseDevices()?.sn??"";
        });
      });
    }
    return Container(
      margin: const EdgeInsets.all(10),
      child: Column(
        children: [
          Container(
            margin: const EdgeInsets.fromLTRB(0, 0, 0, 10),
            child: Row(
              children: [const Text("设备编号："), Text(sn)],
            ),
          ),
          ClipRRect(
            borderRadius: BorderRadius.circular(10),
            child: Container(
              color: Colors.grey,
              margin: const EdgeInsets.fromLTRB(0, 0, 0, 0),
              child: Row(
                children: [
                  TextButton(onPressed: () {}, child: const Text("重启")),
                  Container(
                    margin: const EdgeInsets.fromLTRB(10, 0, 0, 0),
                    child:
                        TextButton(onPressed: () {}, child: const Text("关机")),
                  )
                ],
              ),
            ),
          ),
          Container(
            margin: const EdgeInsets.fromLTRB(0, 20, 0, 0),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(10),
              child: Container(
                color: Colors.cyan,
                alignment: Alignment.topLeft,
                padding: EdgeInsets.all(10),
                width: MediaQuery.of(context).size.width,
                child: Column(
                  children: [
                    Row(
                      children: [
                        Expanded(
                            child: Container(
                          width: MediaQuery.of(context).size.width,
                          height: 40,
                          child: TextField(
                            keyboardType: TextInputType.text,
                            controller: _countryTextEditingController,
                            decoration: const InputDecoration(
                              labelText: "国家",
                              border: OutlineInputBorder(),
                            ),
                            onEditingComplete: () {},
                          ),
                        )),
                        Expanded(
                          child: Container(
                            width: MediaQuery.of(context).size.width,
                            height: 40,
                            margin: EdgeInsets.fromLTRB(20, 0, 0, 0),
                            child: TextField(
                              keyboardType: TextInputType.text,
                              controller: _modelTextEditingController,
                              decoration: const InputDecoration(
                                labelText: "MODEL",
                                border: OutlineInputBorder(),
                              ),
                              onEditingComplete: () {},
                            ),
                          ),
                        ),
                        Expanded(
                          child: Container(
                            width: MediaQuery.of(context).size.width,
                            height: 40,
                            margin: EdgeInsets.fromLTRB(20, 0, 0, 0),
                            child: TextField(
                              keyboardType: TextInputType.text,
                              controller: _gaidTextEditingController,
                              decoration: const InputDecoration(
                                labelText: "GAID",
                                border: OutlineInputBorder(),
                              ),
                              onEditingComplete: () {},
                            ),
                          ),
                        ),
                        Expanded(
                            child: Container(
                          width: MediaQuery.of(context).size.width,
                          height: 40,
                          margin: EdgeInsets.fromLTRB(20, 0, 0, 0),
                          child: TextField(
                            keyboardType: TextInputType.text,
                            controller: _versionTextEditingController,
                            decoration: const InputDecoration(
                              labelText: "版本",
                              border: OutlineInputBorder(),
                            ),
                            onEditingComplete: () {},
                          ),
                        )),
                        Expanded(
                            child: Container(
                          width: MediaQuery.of(context).size.width,
                          height: 40,
                          margin: EdgeInsets.fromLTRB(20, 0, 0, 0),
                          child: TextField(
                            keyboardType: TextInputType.text,
                            controller: _numberTextEditingController,
                            decoration: const InputDecoration(
                              labelText: "条数",
                              border: OutlineInputBorder(),
                            ),
                            onEditingComplete: () {},
                          ),
                        )),
                        Expanded(
                            child: Container(
                          width: MediaQuery.of(context).size.width,
                          height: 40,
                          margin: EdgeInsets.fromLTRB(20, 0, 0, 0),
                          child: TextField(
                            keyboardType: TextInputType.text,
                            controller: _gaidTypeTextEditingController,
                            decoration: const InputDecoration(
                              labelText: "gaidType",
                              border: OutlineInputBorder(),
                            ),
                            onEditingComplete: () {},
                          ),
                        )),
                      ],
                    ),
                    Container(
                      width: 200,
                      alignment: Alignment.center,
                      margin: const EdgeInsets.fromLTRB(0, 20, 0, 0),
                      child: const Row(
                        children: [
                          Text(
                            "请求状态：",
                            textAlign: TextAlign.center,
                          ),
                          Text(
                            "未请求",
                            textAlign: TextAlign.center,
                          )
                        ],
                      ),
                    ),
                    Container(
                      margin: const EdgeInsets.fromLTRB(0, 20, 0, 0),
                      child: SizedBox(
                        width: 200,
                        child: Row(
                          children: [
                            TextButton(
                                onPressed: () {
                                  if (!_isRequestChangeDevice) {
                                    _isRequestChangeDevice = true;
                                    NetManager.RequestDevicesInfo(
                                            _countryTextEditingController
                                                .value.text
                                                .toString(),
                                            _versionTextEditingController
                                                .value.text
                                                .toString(),
                                            _modelTextEditingController
                                                .value.text
                                                .toString(),
                                            _gaidTextEditingController.text
                                                .toString(),
                                            _gaidTypeTextEditingController
                                                .value.text
                                                .toString())
                                        .then((value) => {
                                              debugPrint(
                                                  "请求结果：${value.result}"),
                                              _isRequestChangeDevice = false,
                                      NetManager.RequestChangeBaseInfo(value.result),
                                      NetManager.RequestChangeProp(value.result)
                                            });
                                  }
                                },
                                child: const Text("发起请求")),
                            TextButton(
                                onPressed: () {}, child: const Text("请求详情")),
                          ],
                        ),
                      ),
                    )
                  ],
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}
